//
//  SettingViewController.m
//  SmartWatch
//
//  Created by zwj on 2022/11/22.
//

#import "SettingViewController.h"
#import "LoginViewController.h"


@interface SettingViewController ()

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BACKGROUNDCOLOR;
    self.title = @"设置";
}


- (void)initSubviews {
    [super initSubviews];
    
    MyLinearLayout *root = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Vert];
    root.myHorzMargin = 0;
    root.myTop = 15 + NavigationContentTop;
    root.myHeight = kAutoHeight;
    [self.view addSubview:root];
    
    NSArray *arr = @[@"清理缓存",@"用户协议",@"免责协议"];
    for (int i = 0; i < arr.count; i++) {
        
        MyLinearLayout *item = [MyLinearLayout linearLayoutWithOrientation:MyOrientation_Horz];
        item.gravity =  MyGravity_Vert_Center;
        item.myHeight = 48;
        item.backgroundColor = COLOR_4;
        item.myHorzMargin = 0;
        item.padding = UIEdgeInsetsMake(0, 15, 0, 15);
        item.tag = 100 + i;
        [root addSubview:item];
        [item setTarget:self action:@selector(itemClick:)];
        
        {
            UILabel *name = [UILabel createLabelText:arr[i] font:UIFontMake(15) textColor:COLOR1515];
            name.mySize = kAutoSize;
            name.weight = 1;
            [item addSubview:name];
            
            if(i == 0){
                NSString * currentCacheSize = [self fileSizeWithInteger:[[SDImageCache sharedImageCache] totalDiskSize]];
                UILabel *cache = [UILabel createLabelText:currentCacheSize font:UIFontMake(13) textColor:COLOR666];
                cache.mySize = kAutoSize;
                [item addSubview:cache];
            }
             
            UIImageView *right = [[UIImageView alloc]initWithImage:UIImageMake(@"icon_arrow_right")];
            right.mySize = CGSizeMake(16, 16);
            [item addSubview:right];
        }
        
        if(i < arr.count - 1){
            UIView *line = UIView.new;
            line.myHorzMargin = 0;
            line.myHeight = 0.5;
            line.backgroundColor = HexColor(@"#E3E3E3");
            [root addSubview:line];
        }
    }
    
    UIButton *layout = [UIButton createButtonText:@"退出登录" font:UIFontMake(15) textColor:HexColor(@"#EB5757")];
    layout.myHorzMargin = 0;
    layout.myTop = 20;
    layout.myHeight = 50;
    layout.backgroundColor = COLOR_4;
    [layout addTarget:self action:@selector(layoutClick) forControlEvents:UIControlEventTouchUpInside];
    [root addSubview:layout];
    
}

- (void)itemClick:(UIView *)view {
    NSInteger tag = view.tag - 100;
    
    switch (tag) {
        case 0:
        {
            
            [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
                
            }];
            [[SDImageCache sharedImageCache] clearMemory];
            [QMUITips showLoadingInView:self.view];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [QMUITips hideAllTips];
            });
        }
            break;
            
        default:
            break;
    }
}

- (void)layoutClick {
    [self alertWithTitle:@"退出登录" content:@"确定要退出吗？" sure:@"确定" handler:^{
        [ZWJCache removeUserInfo];
        
        [QMUITips showLoadingInView:self.view];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [QMUITips hideAllTips];
            LoginViewController *vc = [[LoginViewController alloc] init];
            ASNavigationController *nav = [[ASNavigationController alloc] initWithRootViewController:vc];
            [UIApplication sharedApplication].keyWindow.rootViewController = nav;
        });
    }];
}


// 根据数据计算出大小
- (NSString *) fileSizeWithInteger:(NSInteger)size{
    // 1K = 1024dB, 1M = 1024K,1G = 1024M
    if (size < 1024) {// 小于1k
        return [NSString stringWithFormat:@"%ldB",(long)size];
    }else if (size < 1024 * 1024){// 小于1m
        CGFloat aFloat = size/1024;
        return [NSString stringWithFormat:@"%.0fK",aFloat];
    }else if (size < 1024 * 1024 * 1024){// 小于1G
        CGFloat aFloat = size/(1024 * 1024);
        return [NSString stringWithFormat:@"%.1fM",aFloat];
    }else{
        CGFloat aFloat = size/(1024*1024*1024);
        return [NSString stringWithFormat:@"%.1fG",aFloat];
    }
}

@end
